package com.demomaster.quickcompat.drawable;

import android.content.res.ColorStateList;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.RippleDrawable;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class QuickRippleDrawable extends RippleDrawable implements QuickDrawable {
    float[] mRadii;
    float mRadius =0;
    private final Drawable mMaskDrawable;
    /**
     * Creates a new ripple drawable with the specified ripple color and
     * optional content and mask drawables.
     *
     * @param color   The ripple color
     * @param content The content drawable, may be {@code null}
     * @param mask    The mask drawable, may be {@code null}
     */
    public QuickRippleDrawable(@NonNull ColorStateList color, @Nullable Drawable content, @Nullable Drawable mask) {
        super(color, content, mask);

        mMaskDrawable = mask;
    }


    @Override
    public void setCornerRadii(float[] radii){
       // super.setCornerRadii(radii);
        this.mRadii = radii;

    }
    public float[] getCornerRadii(){
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
            return getCornerRadii();
        }else {
            return null;
        }
    }

    @Override
    public void setCornerRadius(float radius){
        //super.setCornerRadius(radius);
        mRadius = radius;
    }

    Paint mStrokePaint;
    ColorStateList mStrokeColors;
    int mStrokeWidth;
    @Override
    public void setStroke(int borderWidth, ColorStateList borderColor) {
        mStrokePaint = new Paint();
        mStrokeColors = borderColor;
        mStrokePaint.setStyle(Paint.Style.STROKE);
        mStrokePaint.setStrokeWidth(borderWidth);
        mStrokeWidth = borderWidth;
    }

    public float getCornerRadius(){
        return mRadius;
    }

    @Override
    public void draw(Canvas canvas) {
       Rect rect = getBounds();
        RectF rectF = new RectF(rect); // 将 Rect 转换为 RectF
        Path path = new Path();
        path.addRoundRect(rectF, mRadius, mRadius, Path.Direction.CCW);
        if(mRadii!=null) {
            path.addRoundRect(rectF, mRadii, Path.Direction.CW);
        }
        canvas.clipPath(path);
        super.draw(canvas);
        if(mStrokePaint!=null) {
            canvas.drawPath(path, mStrokePaint);
        }
    }
}
